package com.jdd.dao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdd.common.api.vo.Result;
import com.jdd.common.system.vo.LoginUser;
import com.jdd.common.util.LoginUserUtils;
import com.jdd.common.util.SnowFlakeUtil;
import com.jdd.dao.entity.TFloor;
import com.jdd.dao.entity.TUnit;
import com.jdd.dao.mapper.TFloorMapper;
import com.jdd.dao.mapper.TUnitMapper;
import com.jdd.dao.service.ITFloorService;
import com.jdd.dao.vo.SpaceVo;
import com.jdd.modules.device.entity.TDevice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ls
 * @since 2022-09-19
 */
@Slf4j
@Service
public class TFloorServiceImpl extends ServiceImpl<TFloorMapper, TFloor> implements ITFloorService {

    @Resource
    private TUnitMapper tUnitMapper;


    @Override
    public Result<?> addNew(SpaceVo space) {
        log.info("-----新增楼层-----");
        Result<?> result = new Result<>();
        LoginUser loginUser = LoginUserUtils.getLoginUser();
        List<String> floors = new ArrayList<>();
        Integer startNumber = space.getStartNumber();
        Integer placeNumbers = space.getNumbers();
        String identCode = space.getIdentCode();
        LambdaQueryWrapper<TFloor> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(TFloor::getUnitId,space.getId());
        int count = this.count(queryWrapper1);
        if (count+placeNumbers>100){
            return result.error500("一个楼层中最多存在100个房间");
        }
        for (int i = 0; i < placeNumbers; i++) {
            floors.add(getFloorName(startNumber,i,identCode,space.getLimitNumber()));
        }
        LambdaQueryWrapper<TFloor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TFloor::getUnitId,space.getId());
        queryWrapper.in(TFloor::getFloorName,floors);
        List<TFloor> list=this.list(queryWrapper);
        if (list.size()>0){
            return result.error500("该区间存在相同楼栋");
        }

        TFloor tFloor;
        TUnit tUnit = tUnitMapper.selectById(space.getId());
        String parentCode = tUnit.getParentCode() + "U" + tUnit.getCode();
        List<TFloor> spaceList = new LinkedList<>();
        for (String floorName : floors){
            tFloor = new TFloor();
            tFloor.setId(SnowFlakeUtil.getId());
            tFloor.setUnitId(space.getId());
            tFloor.setFloorName(floorName);
            tFloor.setCreateUser(loginUser.getId());
            tFloor.setParentCode(parentCode);
            tFloor.setSysProjectCode(tUnit.getSysProjectCode());
            spaceList.add(tFloor);
        }
        this.saveBatch(spaceList);
        log.info("-----新增楼栋成功------{}",spaceList);
        return Result.ok("新增成功！");
    }

    @Override
    public List<TDevice> selectFloorDevice(String floorId) {
        return null;
    }

    private String getFloorName(Integer startNumber, Integer placeNumbers, String identCode,Integer limitNumber) {
        int num = startNumber + placeNumbers;
        if (identCode==null){
            identCode = "";
        }
        String number=num+"";
        while (number.length()<limitNumber){
            number ='0'+number;
        }
        number = identCode+number;
        return number;
//        if (num < 10) {
//            return identCode+"00" +num;
//        } else if (num >= 10 && num < 100) {
//            return identCode+"0" + num;
//        } else if (num >= 100 && num < 1000) {
//            return identCode+"" +num;
//        }  else {
//            return null;
//        }
    }
}
